package com.pureut.retrospect.service.impl;


import com.alibaba.fastjson.JSON;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.retrospect.domain.dto.*;
import com.pureut.retrospect.mapper.MaterialAwmateriaMapper;
import com.pureut.retrospect.mapper.MaterialHalfMapper;
import com.pureut.retrospect.service.MaterialTraceabilityService;
import com.pureut.system.api.QualityFeginService;
import com.pureut.system.api.StorageFegin;
import com.pureut.system.api.SystemDocumentFeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/4/11 9:38
 * @Desc: 物料追溯
 */
@Service
public class MaterialTraceabilityServiceImpl implements MaterialTraceabilityService {

    /**
     * 检验单远程feign
     */
    @Resource
    private QualityFeginService qualityFeginService;

    /**
     * 仓库远程feign
     */
    @Resource
    private StorageFegin storageFegin;

    /**
     * 原料信息
     */
    @Resource
    private MaterialAwmateriaMapper materialAwmateriaMapper;

    /**
     * 半成品信息
     */
    @Resource
    private MaterialHalfMapper materialHalfMapper;

    /**
     * 单据配置
     */
    @Resource
    private SystemDocumentFeignService systemDocumentFeignService;

    /**
     * 物料追溯基础信息
     *
     * @param wsiCategory, wsiMaterialNumber
     * @return
     */
    @Override
    public List<RetrospectManageDto> getFoundationList(Long wsiCategory, String wsiMaterialNumber) {
        AjaxResult retrospectManageList = storageFegin.getRetrospectManageList(wsiCategory, wsiMaterialNumber);
        List<RetrospectManageDto> data = JSON.parseArray(JSON.toJSONString(retrospectManageList.get("data")), RetrospectManageDto.class);

        if (data == null) {
            throw new GlobalException("非法号码,请输入正确号码再次查询;");
        }

        for (RetrospectManageDto retrospectManageDto : data) {
            // 当单据类别为原料时
            if (Long.parseLong(retrospectManageDto.getWsiCategory()) == 1) {
                // 原料详情
                MaterialAwmateriaDto materialAwmateriaInfo = materialAwmateriaMapper.getMaterialAwmateriaInfo(retrospectManageDto.getWsiMaterialCode());

                GregorianCalendar calendar = new GregorianCalendar();
                if (materialAwmateriaInfo != null) {
                    if (retrospectManageDto.getWsiReveiveTime() != null) {
                        // 复检日期=入库时间（库存汇总）+复检日期（基础配置）
                        calendar.setTime(retrospectManageDto.getWsiReveiveTime());
                        calendar.add(Calendar.DATE, materialAwmateriaInfo.getMaterialInspection());
                        Date wsiRecheckTime = calendar.getTime();
                        retrospectManageDto.setWsiRecheckTime(wsiRecheckTime);
                    }

                    if (retrospectManageDto.getWsiProducedDate() != null) {
                        // 过保日期=生产日期（库存汇总）+保质期（基础配置）
                        calendar.setTime(retrospectManageDto.getWsiProducedDate());
                        calendar.add(Calendar.DATE, materialAwmateriaInfo.getMaterialQuality());
                        Date wsiExpirationTime = calendar.getTime();
                        retrospectManageDto.setWsiExpirationTime(wsiExpirationTime);
                    }
                }
            }
            // 当单据类别为半成品时
            if (Long.parseLong(retrospectManageDto.getWsiCategory()) == 3) {
                // 半成品详情
                MaterialHalfDto materialHalfInfo = materialHalfMapper.getMaterialHalfInfo(retrospectManageDto.getWsiMaterialCode());

                GregorianCalendar calendar = new GregorianCalendar();
                if (retrospectManageDto.getWsiReveiveTime() != null) {
                    // 复检日期=入库时间（库存汇总）+复检日期（基础配置）
                    calendar.setTime(retrospectManageDto.getWsiReveiveTime());
                    calendar.add(Calendar.DATE, materialHalfInfo.getMaterialInspection());
                    Date wsiRecheckTime = calendar.getTime();
                    retrospectManageDto.setWsiRecheckTime(wsiRecheckTime);
                }
                if (retrospectManageDto.getWsiProducedDate() != null) {
                    // 过保日期=生产日期（库存汇总）+保质期（基础配置）
                    calendar.setTime(retrospectManageDto.getWsiProducedDate());
                    calendar.add(Calendar.DATE, materialHalfInfo.getMaterialQuality());
                    Date wsiExpirationTime = calendar.getTime();
                    retrospectManageDto.setWsiExpirationTime(wsiExpirationTime);
                }
            }
        }

        return data;
    }

    /**
     * 品检信息
     *
     * @param wsiMaterialNumber
     * @return
     */
    @Override
    public List<MaterialQualityDto> getQualityTemporaryList(String wsiMaterialNumber) {
        AjaxResult qualityTemporaryList = qualityFeginService.getQualityTemporaryList(wsiMaterialNumber);
        List<MaterialQualityDto> data = JSON.parseArray(JSON.toJSONString(qualityTemporaryList.get("data")), MaterialQualityDto.class);
        return data;
    }

    /**
     * 同批次物料
     *
     * @param wsiMaterialNumber
     * @return
     */
    @Override
    public List<RetrospectMaterialDto> getSameBatchList(String wsiMaterialNumber) {
        AjaxResult retrospectMaterialList = storageFegin.getRetrospectMaterialList(wsiMaterialNumber);
        List<RetrospectMaterialDto> data = JSON.parseArray(JSON.toJSONString(retrospectMaterialList.get("data")), RetrospectMaterialDto.class);
        return data;
    }

    /**
     * 查询物料履历
     *
     * @param wsiMaterialNumber
     * @return
     */
    @Override
    public List<MaterialResumeDto> MaterialResumeList(Long wsiCategory, String wsiMaterialNumber) {
        AjaxResult ajaxResult = storageFegin.MaterialResumeList(wsiCategory, wsiMaterialNumber);
        List<MaterialResumeDto> data = JSON.parseArray(JSON.toJSONString(ajaxResult.get("data")), MaterialResumeDto.class);

//        // 作业类型
//        List<SysDictData> jobTypeGrade = DictUtils.getDictCache("job_type");
//        Map<String, String> jobTypeGradeMap = jobTypeGrade.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        if (data!=null){
//            for (MaterialResumeDto materialResumeDto : data){
//                // 作业类型
//                materialResumeDto.setJobTypeDict(jobTypeGradeMap.get(materialResumeDto.getJobType()));
//            }
//        }

        return data;
    }


}
